package app.v1.schedulers;

import app.common.utils.YdApi;
import app.common.utils.cjt.CJtUtils;
import app.v1.wdt.mapper.WdtSalesOrderDetailsMapper;
import app.v1.wdt.mapper.WdtSalesOrderIdGeneratorMapper;
import app.v1.wdt.mapper.WdtSalesOrderMapper;
import app.v1.wdt.service.WdtSalesOrderDetailsService;
import com.gitee.feizns.dynamic.DateUtils;
import com.gitee.feizns.quickstart.spring.AppUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * @author feizns
 * @since 2023/9/20
 */
@Component
@RequiredArgsConstructor
public class AutoStatisticsSalesOrderDetailsScheduler {

    /**
     *
     */
    private final WdtSalesOrderDetailsService wdtSalesOrderDetailsService;

    /**
     * 映射器
     */
    private final WdtSalesOrderDetailsMapper mapper;

//    /**
//     * （每日凌晨12:30点抓取一次数据）
//     */
//    @Scheduled(cron = "0 30 0 * * ?")
//    public void autoPullSalesOrderDetailsFromQmWdt() {
//        //昨天
//        LocalDate yesterday = LocalDate.now().minusDays(1);
//    }

    /**
     * 自动统计销售订单细节（每日凌晨05:00点统计一次数据）
     */
    @Scheduled(cron = "0 0 5 * * ?")
    public void autoStatisticsSalesOrderDetails() {
        //昨天
        LocalDate yesterday = LocalDate.now().minusDays(1);
        //抓取昨日的单品销售日数据
        wdtSalesOrderDetailsService.pullSalesOrderDetailsFromQmWdt(yesterday);
        //统计昨天发货的数据
        wdtSalesOrderDetailsService.statisticsSalesOrderDetails(mapper.groupingByConsignDate(yesterday));
        //删除90天之前的数据
        mapper.deleteCreateBeforeDate(yesterday.minusDays(120));
    }

    /**
     * 统计指定日期内的数据
     * @param startDate {@link LocalDate}
     * @param endDate {@link LocalDate}
     */
    public void statisticsSalesOrderDetails(LocalDate startDate, LocalDate endDate) {
        while ( startDate.isBefore(endDate) ) {
            recaptureSalesOrderDetailsFromQmWdt(startDate);
            startDate = startDate.plusDays(1);
        }
    }

    /**
     * 重新抓取单品销售日统计数据
     * @param date {@link LocalDate}
     */
    public void recaptureSalesOrderDetailsFromQmWdt(LocalDate date) {
        //删除指定单品销售日数据
        int count = mapper.deleteByConsignDate(date);
        System.out.println("删除 [" + date + "] 数据：" + count);
        //抓取单品销售日数据
        reimportSalesOrder(date);
    }

    /**
     * 重新导入单品销售日统计数据
     * @param date 重新导入的日期
     */
    public void reimportSalesOrder(LocalDate date) {
        //删除畅捷通中的数据
        CJtUtils.deleteSalesDelivery(date, date);
        //删除宜搭中的数据
        YdApi.deleteYiDaUtils(date, date.plusDays(1));
        YdApi.deleteYiDaUtilsGroupByWarehouseNo(date, date.plusDays(1));
        //删除自动生成的 i 序列
        AppUtils.getBean(WdtSalesOrderMapper.class).deleteByOrderNoDate(DateUtils.format(date, "yyyyMMdd"));
        //删除自动生成的 i 序列
        AppUtils.getBean(WdtSalesOrderIdGeneratorMapper.class).deleteByDate(date);
        //导入数据
        wdtSalesOrderDetailsService.statisticsSalesOrderDetails(mapper.groupingByConsignDate(date));
        //输出
        System.out.println("单品销售日统计：" + date + " 日的数据已成功接入.");
        System.out.println("单品销售日统计：" + date + " 日的数据已成功接入.");
        System.out.println("单品销售日统计：" + date + " 日的数据已成功接入.");
    }

}
